package com.thiscc.sum.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mongodb.DBObject;
import com.thiscc.sum.logic.SumLogic;
import com.thiscc.sum.utils.RequestUtils;
import com.thiscc.sum.utils.ResponseUtils;
import com.thiscc.sum.utils.StringUtils;

@Controller
@RequestMapping("sum")
public class SumController extends CommonController {

    @Autowired
    private SumLogic sumLogic;

    /**
     * 根据条件读取工作总结
     * @param request
     *     	uuid：看总结人的uuid
     * 		state：状态 "0":未读 ,"1":所有
     * @param response	返回符合条件的工作总结
     * @author helios 2014-12-13
     */
    @RequestMapping("readSummary")
    public void readSummary(HttpServletRequest request, HttpServletResponse response) {

        String resultList = sumLogic.readSummary(request);
        ResponseUtils.send(response, resultList);
    }

    /*------------------------------------------------------------------------------*/
    @RequestMapping("setread")
    public void setread(HttpServletRequest request, HttpServletResponse response) {

        boolean resultList = sumLogic.setread(request);

        ResponseUtils.send(response, String.valueOf(resultList));
    }

    /*-------------------------------------------------------------------------------------------*/
    /**
     * 根据条件读取工作总结
     * @param request
     *     	s_id：看总结的_id
     * @author 易泉 2014-12-20
     */
    @RequestMapping("queryByid")
    public void queryByid(HttpServletRequest request, HttpServletResponse response) {

        DBObject DBO_resultList = sumLogic.queryByid(request);

        ResponseUtils.send(response, DBO_resultList.toString());

    }

    /*------------------------------------------------------------------------------*/
    /**
     * 查看这篇总结的 阅读情况 接口
     * @author阳二华  2014-12-16
     * @param request	year：总结截止日期的年份
     * 					month：总结的截止的月份
     *                  day：总结截止的日子
     *                  uuid：这个人的uuid
     *                  classify ：  1日总结 2周总结 3月总结
     */
    @RequestMapping("selectAllIfRead")
    public void selectAllIfRead(HttpServletRequest request, HttpServletResponse response) {

        String result = sumLogic.selectAllIfRead(request);
        ResponseUtils.send(response, result);

    }

    /*------------------------------------------------------------------------------*/
    /**
     * 回复别人给我的评价，做更新。
     * 
     * @author阳二华  2014-12-16
     * @param request	uuid：写回复人uuid
     * 					_id：Summarize_replay表的id
     *                 value回复的内容
     * @param response	ture成功 false失败
     */

    @RequestMapping("replyOthers")
    public void updateReplyOthers(HttpServletRequest request, HttpServletResponse response) {

        String flag = sumLogic.updateReplyOthers(request);
        ResponseUtils.send(response, flag);
    }

    /*------------------------------------------------------------------------------*/
    /**
     * 第一次写回复
     * @param request	
     * "param":{"oid":"","replayno":"","content":"",
     * "towards":[{ "title" : "我要贡献",
      "value" : "尽快转正",
      "lev" : "1"}]}
     * @param response
     * @author helios
     * 获取总结编号、回复人编号、回复内容、回复哪一条
     */
    @RequestMapping("firstReply")
    public void firstReply(HttpServletRequest request, HttpServletResponse response) {

        String ret = sumLogic.firstReply(request);
        ResponseUtils.send(response, ret);
    }

    /*------------------------------------------------------------------------------*/
    /**
     * 阳二华 2014-12-13   获取工作总结模板
     * 
     * @param request
     *            写总结人的uuid
     * @param response
     *            获取到的工作模板
     */
    @RequestMapping("getTemplate")
    public void getTemplate(HttpServletRequest request, HttpServletResponse response) {

        String jsonObject = sumLogic.getTemplate(request);
        ResponseUtils.send(response, jsonObject);

    }

    /*------------------------------------------------------------------------------*/
    /**
     * 阳二华 2014-12-13 添加总结 
     * 
     * @param dbObject
     * @author Einstein Ron()
     */
    @RequestMapping("addSumData")
    public void addSumData(HttpServletRequest request, HttpServletResponse response) {

        String flag = sumLogic.addSumData(request);
        ResponseUtils.send(response, flag);

    }

    /*-------------------------------------------------------------------------------------------*/
    /**
     *	我回复的
     * @param uuid //拿到登录人的uuid
     * @param pageNo //当前页数
     * @param perPageCount //总记录数
     * @author 易泉 2014-12-13 
     */
    @RequestMapping("meReply")
    public void meReply(HttpServletRequest request, HttpServletResponse response) {

        String s_result = sumLogic.meReply(request);
        ResponseUtils.send(response, s_result);
    }

    /*-------------------------------------------------------------------------------------------*/
    /**
     *	回复我的
     * @param ccno //拿到登录人的CCNO号
     * @param pageNo //当前页数
     * @param perPageCount 总记录数
     * @author 易泉 2014-12-13 
     */
    @RequestMapping("replyMe")
    public void replyMe(HttpServletRequest request, HttpServletResponse response) {

        String s_result = sumLogic.replyMe(request);
        ResponseUtils.send(response, s_result);
    }

    /*-------------------------------------------------------------------------------------------*/
    /**
     *	回复页面的回复我的总条数 replyMeCount
     *	回复页面的我回复的总条数 meReplyCount
     * @param uuid //拿到登录人的UUID
     * @param 回复我的  功能点参数ryMePid
     * @param 我回复的  功能点参数meRyPid
     * @author 易泉 2014-12-13 
     */
    @RequestMapping("replyCount")
    public void replyCount(HttpServletRequest request, HttpServletResponse response) {

        String s_ret = sumLogic.replyCount(request);
        ResponseUtils.send(response, s_ret);
    }

    /*------------------------------------------------------------------------------*/
    /**
     * 获得某总结的所有回复内容
     * @param request
     * @param response
     * @author DK
     */
    @RequestMapping("getReplays")
    public void getReplays(HttpServletRequest request, HttpServletResponse response) {

        String result = sumLogic.getReplays(request);
        ResponseUtils.send(response, result);
    }

    /*------------------------------------------------------------------------------*/
    /**
     * 得到总结的相关信息,是否完成，阅我人数，是否分享，回复数
     * @param request type:index；表示是刚进来时的加载,change:表示是手动切换年月
     * @param response 
     * @createTime 2014-12-16
     * @since 1.0
     * @author Einstein  
     */
    @RequestMapping("getSummaryInfo")
    public void getSummaryInfo(HttpServletRequest request, HttpServletResponse response) {

        String s_uuid = request.getParameter("uuid");//uuid
        if (StringUtils.isBlank(s_uuid) || "null".equals(s_uuid)) {
            s_uuid = (String) request.getSession().getAttribute("uuid");
        }
        String s_type = request.getParameter("type");//type:index；表示是刚进来时的加载,change:表示是手动切换年月,手动切换周这个type为null
        int i_year = RequestUtils.getInt(request, "year");//年
        int i_month = RequestUtils.getInt(request, "month");//月
        int i_week = RequestUtils.getInt(request, "week");//周 
        ResponseUtils.send(response, sumLogic.getSummaryInfo(s_uuid, s_type, i_year, i_month, i_week).toString());
    }

    /*------------------------------------------------------------------------------*/

    /**
     * 跟据传入的公司编号、部门uuid查出当前人所在公司的所有员工
     * @param request companyno:公司编号
     * 					uuid：部门uuid
     * @param response
     * @author helios
     * @createtime 2014-12-15
     */
    @RequestMapping("getPeople")
    public void getPeople(HttpServletRequest request, HttpServletResponse response) {

        List<DBObject> retList = sumLogic.getPeople(request);
        ResponseUtils.send(response, retList.toString());
    }

    /*------------------------------------------------------------------------------*/
    /**
     * 获取分组信息
     * @param request 参数：无 从session中获取当前登录人的uuid
     * @param response 返回获取的分组信息数组，分组名、人数、是否置顶
     * @author helios
     */
    @RequestMapping("groupList")
    public void groupList(HttpServletRequest request, HttpServletResponse response) {

        List<DBObject> retList = sumLogic.groupList(request);
        ResponseUtils.send(response, retList.toString());
    }

    /*------------------------------------------------------------------------------*/
    /**
     * 
     * @param request 参数：无 从session中获取当前登录人的uuid
     * @param response 返回登录人的接收人uuid,ccno,name
     * @author Einstein
     */
    @RequestMapping("getReciever")
    public void getReciever(HttpServletRequest request, HttpServletResponse response) {

        List<DBObject> l_reciever = sumLogic.getReciever(request);
        ResponseUtils.send(response, l_reciever.toString());
    }

    /**
     * 初始化版本号
     * @author helios
     * @createtime 2015-1-3
     * @since 1.0
     */
    @RequestMapping("initVersion")
    public void initVersion() {

        sumLogic.initVersion();
    }

    /*------------------------------------------------------------------------------*/
    /**
     * 总结报表
     * @param request 参数：日期
     * @param response 返回应交人数，已交人数，迟交人数，迟交人名，未交人数，未交人名
     * @author Dimon
     */
    @RequestMapping("getSumCount")
    public void getSumCount(HttpServletRequest request, HttpServletResponse response) {

        JSONObject retList = sumLogic.getSumCount(request);
        ResponseUtils.send(response, retList.toString());
    }

    /*------------------------------------------------------------------------------*/
}
